<!-- 商品列表 -->
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            class="goods-search-wrap"
            v-model="query"
            @keyup.enter.native="searchGoods"
            clearable
            @clear="clearSearch"
            placeholder="请输入内容"
          >
            <el-select v-model="selectSearchName" slot="prepend" placeholder="请选择">
              <el-option label="按名称" value="name" key="name"></el-option>
              <el-option label="按描述" value="desc" key="desc"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品数据表格 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="name" width="300px"></el-table-column>
        <el-table-column label="商品价格" prop="price" width="100px"></el-table-column>
        <el-table-column label="商品描述" prop="descs"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.id)"
            >删除</el-button>
            <el-button type="danger" icon="el-icon-edit" size="mini">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { reqGoodsList, reqDeleteGoods } from '@/api'
export default {
  data() {
    return {
      queryInfo: {
        searchName: '',
        searchDescs: '',
        pageNum: 1,
        pageSize: 5
      },
      query: '', // 搜索的值
      goodsList: [], // 商品列表
      total: 0, // 总页码
      selectSearchName: 'name' // 选择模糊查询条件
    }
  },

  created() {
    this.getGoodsList()
  },

  methods: {
    // 获取所有商品列表
    async getGoodsList() {
      const result = await reqGoodsList(this.queryInfo)
      if (result.code === 200) {
        console.log(result.data)
        this.goodsList = result.data
        this.total = result.total
        this.queryInfo.pageNum = result.pageNum
        this.queryInfo.pageSize = result.pageSize
      } else {
        this.$message.error('获取商品失败！')
      }
    },

    // 清除模糊搜索
    clearSearch() {
      this.queryInfo.searchName = ''
      this.queryInfo.searchDescs = ''
      this.getGoodsList()
    },

    // 点击搜索
    searchGoods() {
      if (this.selectSearchName === 'name') {
        this.queryInfo.searchName = this.query
        this.queryInfo.searchDescs = ''
      } else {
        this.queryInfo.searchName = ''
        this.queryInfo.searchDescs = this.query
      }
      this.getGoodsList()
    },

    // 添加商品
    addGoods() {
      this.$router.push('/products/add')
    },

    // 删除商品
    deleteGoods(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const result = await reqDeleteGoods(id)
          if (result.code === 204) {
            this.$message.success('删除商品成功！')
          } else {
            this.$message.error('删除商品失败！')
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 分页每页显示条数改变时
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getGoodsList()
    },

    // 当前分页改变时
    handleCurrentChange(newNum) {
      this.queryInfo.pageNum = newNum
      this.getGoodsList()
    }
  }
}
</script>
<style lang='less' scoped>
.goods-search-wrap /deep/.el-select .el-input {
  width: 100px;
}
</style>
